*******************************************************************************
*******************************************************************************
* Replication materials for "Resilient Democracies"
*******************************************************************************
*******************************************************************************


*---------------------- Introduction ------------------------------------------

This replication archive contains all material necessary to replicate the 
results reported in the paper. This README describes the necessary software 
to be installed to carry out the replication [section 1]. It lists the 
computing environment that was used to obtain the results reported in the 
paper [section 2] and provides a brief description of the file naming convention 
used in the archive [section 3].

The ZIP file "CODE.zip" contains the code.
The ZIP file "CODE_with_all_output.zip" contains the code and all output 
(.Rout files, .log files, .pdf files, .tex files) generated by running
it on a Mac OS ARM (M2) system.


*------------------- Running the replication ----------------------------------

On Linux/MacOS, after installing all required software and packages, the 
complete replication can be carried out by calling the following shell script:

__run__.sh 

This will run all models, generate Figure PDFs and write estimates to files. 
A latex run produces a PDF file called AllFiguresTables.pdf  which produces all 
figures from the main text of the paper and all appendix tables and figures.


[On Windows, the corresponding script is __run__.bat. You can run the batch file 
by double-clicking which should open a command shell. Please note that execution 
on Windows will take *considerably* longer due to limitations in the parallel 
processing of some code. Note that the results in this repository were created on 
an ARM CPU Mac OS system]



*******************************************************************************
*    [1]   Necessary Software
*******************************************************************************

To execute the scripts you need to have the following installed and working:

(1) R (version 4.4.3)

    (1a)
    Download and install R from www.r-project.org:
    https://cran.r-project.org/bin/windows/base/
    
    (1b)
    Ensure that you can compile R packages containing C++ code from source. 
    On Mac/Unix this simply requires a working toolchain.  On Mac OS, you can 
    install the Xcode command line tools in the terminal via  
    xcode-select --install . Please see https://mac.r-project.org/tools/ for 
    details. On Windows, you need to install Rtools. See 
    https://cran.r-project.org/bin/windows/Rtools/rtools44/files/
    D/l and install rtools44-xxx.exe file. At the time of writing, the
    file is called rtools44-6459-6401.exe.
      
    (1c)
    Within R, install the following packages:
    pkgs <- c("coxme", "readstata13", "car", "data.table", "forecast", "imputeTS", 
    "doRNG", "coda", "MBA", "mvtnorm", "sp", "knitr", "sf", "tidyr", "dplyr", 
    "ggplot2", "doParallel", "spdep")
    install.packages(pkgs)
    
    (1d) bamlss_1.1-9.tar.gz. This local file (which includes custom code) is 
    installed when running the __run__.sh  [or __run__.bat] script. 
    Should the scripted installation fail, you can install the package manually 
    by running the following command in R (ansure that your working directory  
    contains the bamlss_1.1-9.tar.gz file):
    install.packages("bamlss_1.1-9.tar.gz", repos = NULL, type = "source")
    

(2) Stata (version 18)
    (i)  Install Stata
    (ii) Within Stata, install the follwing packages:
         ssc install stpm2
         ssc install rcsgen
         ssc install stpm2_standsurv
         ssc install moremata
         ssc install reghdfe
         ssc install regife
         ssc install ftools
         ssc install carryforward
         ssc install fre
         ssc install hdfe

(3) LaTeX

    A working LaTeX installation (pdflatex) is required to compile the 
    tables (which are created via knitr from stored estimates).




*******************************************************************************
*             [2] Computing environment used
*******************************************************************************

* This section lists the computing environment and software versions used to 
compute the results presented in the paper and appendix.

Apple M2 Ultra CPU (16 P cores), 64GB RAM
Mac OS 15.3.2.

R, version 4.4.3
Stata MP/8, version 18

Expected run time: ca 8 hrs.


Versions of R packages used:

Package            Version
---------------------------

spdep        1.3-10
spData       2.3.4
doParallel   1.0.17
iterators    1.0.14
ggplot2      3.5.1
dplyr        1.1.4
tidyr        1.3.1
sf           1.0-19
knitr        1.50
sp           2.2-0
mvtnorm      1.3-3
MBA          0.1-2
coda         0.19-4.1
doRNG        1.8.6.1
rngtools     1.5.2
foreach      1.5.2
imputeTS     3.3
forecast     8.23.0
data.table   1.17.0
car          3.1-3
carData      3.0-5
readstata13  0.10.1
coxme        2.2-22
bdsmatrix    1.3-7
survival     3.8-3
bamlss       1.1-9 


Versions of Stata packages used:

Package           Version
---------------------------
stpm2              1.7.6 
rcsgen             1.5.9 
stpm2_standsurv    1.1.2
moremata           2.0.2
reghdfe            6.12.3
regife             0.4
ftools             2.49.1
carryforward       4.5
fre                1.2.5
hdfe               3.2.9



*********************************************************************
*    [3]   File naming convention
*********************************************************************

SU      Survival models
SUNP    Survival models estimated nonparametrically
SY      Models for V-Dem symptoms of backsliding
SYSU    Joint models of symptoms and survival

